layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'invoice_finance_list',
		form_filter = 'invoice_finance_search_form';
	// 渲染表格
	if (load_data === true) {
		table.render({
			elem: '#' + table_filter,
			id: table_filter,
			url: invoice_list_url,
			method: 'post',
			height: 'full-200',
			where: {
				_o: 'invoice_id',
				_b: 'desc'
			},
			cols: [invoice_finance_cols],
			page: {
				layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
				limit: 30,
				theme: '#393d49',
				groups: 8
			},
			autoSort: false,
			toolbar: false,
			defaultToolbar: [],
			initSort: {
				field: 'invoice_id',
				type: 'desc'
			}
		});
	}

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	var select_search_list = function(field, value) {
		var where = {};
		where[field] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	};
	// select 查询
	form.on('select(builder_type)', function(data) {
		select_search_list('builder_type', data.value);
	})
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search_list(name, value);
	});

	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var invoice_id = data.invoice_id;
		switch (e) {
			case 'recieve_view':
				// 发票明细
				layer.open({
					type: 2,
					anim: -1,
					title: '发票明细',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '70%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: invoice_view_url + '?invoice_id=' + invoice_id
				})
				return false;
				break;
			case 'make_invoice':
				// 开票
				layer.open({
					type: 2,
					anim: -1,
					title: '开具发票',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '70%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: invoice_detail_url + '?invoice_id=' + invoice_id
				})
				return false;
				break;
			case 'return_invoice':
				// 驳回申请
				layer.prompt({
					title: '请输入驳回原因'
				}, function(value, index, elem) {
					var post_data = {
						invoice_id: invoice_id,
						field: 'state',
						value: 3,
						reject_reason: value
					};
					send_info(invoice_save_invoice_url, post_data, function(data) {
						layer.msg('发票申请已被驳回', {
							icon: 1,
							anim: 6
						});
						table.reload('invoice_finance_list');
						layer.close(index);

					})

				});
				break;
			case 'set_draw':
				// 领取发票
				layer.prompt({
					title: '请输入发票领取码'
				}, function(value, index, elem) {
					var post_data = {
						invoice_id: invoice_id
					};
					if (value.length != 4) {
						layer.msg('请输入4位数字领取码', {
							icon: 2,
							anim: 6,
							offset: '60%'
						});
						return false;
					}
					send_info(invoice_find_url, post_data, function(data) {
						if (data.data.draw_code != value) {
							layer.msg('领取码错误', {
								icon: 2,
								anim: 6,
								offset: '60%'
							});
							return false;
						} else {
							var post_data = {
								invoice_id: invoice_id,
								field: 'draw_state',
								value: '1'
							};
							send_info(invoice_save_invoice_url, post_data, function() {
								layer.msg('发票已被领取', {
									icon: 1,
									anim: 6
								});
								table.reload('invoice_finance_list');
								layer.close(index);
							})

						}

					})

				});
				break;
			default:
				break;
		}
	})
})
